In [1]:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
import seaborn as sns
sns.set(font_scale=2)
plt.style.use('seaborn-whitegrid')
plt.rcParams['figure.figsize'] = (6, 6.0)

Make a grid of data

What does this do? It makes nx points on each x-line and ny points on each y-line.

In [2]:
nx = 100
ny = 100
x = np.linspace(0,1,nx)
y = np.linspace(0,1,ny)

gridlines = np.linspace(0,1,8)
data = []
for g in gridlines:
    data.append(np.vstack((x,0*y+g)))
    data.append(np.vstack((0*x+g,y)))
data = np.hstack(list(data))
print(data.shape)
(2, 1600)
In [3]:
plt.plot(data[0,:], data[1,:], 'go')
Out[3]:
[<matplotlib.lines.Line2D at 0x1a189b1550>]

Try some transformations

https://en.wikipedia.org/wiki/Matrix_(mathematics)#Linear_transformations

Shear: $$ \begin{bmatrix} 1 & 1.25 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x\\y \end{bmatrix} $$

Reflection: $$ \begin{bmatrix} -1 & 0 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} x\\y \end{bmatrix} $$

Shrink: $$ \begin{bmatrix} 3/2 & 0 \\ 0 & 2/3 \end{bmatrix} \begin{bmatrix} x\\y \end{bmatrix} $$

Expand: $$ \begin{bmatrix} 3/2 & 0 \\ 0 & 3/2 \end{bmatrix} \begin{bmatrix} x\\y \end{bmatrix} $$

Rotate: $$ \begin{bmatrix} \cos(\pi / 6) & -\sin(\pi / 6)\\ \sin(\pi / 6) & \cos(\pi / 6) \end{bmatrix} \begin{bmatrix} x\\y \end{bmatrix} $$

Move: $$ \begin{bmatrix} 1 & 0\\ 0 & 1\\ \end{bmatrix} \begin{bmatrix} x\\y \end{bmatrix}

  • \begin{bmatrix} xshift\\ yshift \end{bmatrix} $$

Shear

In [9]:
A = np.array([[2,0],[0,1]])
print(A)

newdata = A.dot(data)
plt.plot(newdata[0,:], newdata[1,:], 'bo')
plt.plot(data[0,:], data[1,:], 'go')
plt.axis('square')
[[2 0]
 [0 1]]
Out[9]:
(-0.10000000000000001,
 2.1000000000000001,
 -0.050000000000000003,
 2.1500000000000004)

Reflect

In [13]:
A = np.array([[-1,0],[0,-0.5]])
print(A)

newdata = A.dot(data)
plt.plot(newdata[0,:], newdata[1,:], 'bo')
plt.plot(data[0,:], data[1,:], 'go')
plt.axis('square')
[[-1.   0. ]
 [ 0.  -0.5]]
Out[13]:
(-1.1000000000000001,
 1.1000000000000001,
 -0.57499999999999996,
 1.6250000000000002)

Shrink

In [ ]:
A = np.array([[0.5,0],[0,0.3]])
print(A)

newdata = A.dot(data)
plt.plot(newdata[0,:], newdata[1,:], 'bo')
plt.plot(data[0,:], data[1,:], 'go')
plt.axis('square')

Expand

In [17]:
A = np.array([[2,0],[0,1.5]])
print(A)

newdata = A.dot(data)
plt.plot(data[0,:], data[1,:], 'go')
plt.plot(newdata[0,:], newdata[1,:], 'bo')
plt.axis('square')
[[ 2.   0. ]
 [ 0.   1.5]]
Out[17]:
(-0.10000000000000001, 2.1000000000000001, -0.075000000000000011, 2.125)

Rotate

In [19]:
theta = np.pi/2
A = np.array([[np.cos(theta),-np.sin(theta)],[np.sin(theta),np.cos(theta)]])
print(A)

newdata = A.dot(data)
plt.plot(newdata[0,:], newdata[1,:], 'bo')
plt.plot(data[0,:], data[1,:], 'go')
plt.axis('square')
[[  6.12323400e-17  -1.00000000e+00]
 [  1.00000000e+00   6.12323400e-17]]
Out[19]:
(-1.1000000000000001,
 1.1000000000000001,
 -0.050000000000000003,
 2.1500000000000004)

Shift

Be careful here!

In [ ]:
A = np.array([[1,0],[0,1]])
print(A)

newdata = A.dot(data) + np.array([[0.6],[1.1]])
plt.plot(newdata[0,:], newdata[1,:], 'bo')
plt.plot(data[0,:], data[1,:], 'go')
plt.axis('square')